<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <title></title>
    <link rel="shortcut icon" href="../ff.ico">
    <link rel="stylesheet" href="../Pear-Admin-Layui/component/pear/css/pear.css"/>
</head>
<body>
<body class="pear-container">

<div class="layui-card">
    <div class="layui-card-body">
        <table id="user-table" lay-filter="user-table"></table>
    </div>
</div>
</body>

<script type="text/html" id="user-toolbar">
    <button class="pear-btn pear-btn-primary pear-btn-md" lay-event="add">
        <i class="layui-icon layui-icon-add-1"></i>
        新增
    </button>
</script>

<script type="text/html" id="user-bar">
    <button class="pear-btn pear-btn-primary pear-btn-sm" lay-event="edit"><i class="layui-icon layui-icon-edit"></i>
    </button>
    <button class="pear-btn pear-btn-danger pear-btn-sm" lay-event="remove"><i class="layui-icon layui-icon-delete"></i>
    </button>
</script>


<script type="text/html" id="user-sex">
    <input type="checkbox" name="state" value="{{d.thingId}}" lay-skin="switch" lay-text="完成|未完成" lay-filter="sexDemo"
           {{ d.state== 1 ? 'checked' : '' }}>
</script>

<script type="text/html" id="user-login">
    {{#if (d.state == 1) { }}
    <span style="text-decoration: line-through;color: #aaaaaa;">{{d.thing}}</span>
    {{# }else { }}
    <span style="color: #000000">{{d.thing}}</span>
    {{# } }}
</script>


<script src="../Pear-Admin-Layui/component/layui/layui.js"></script>
<script src="../Pear-Admin-Layui/component/pear/pear.js"></script>
<script>
    layui.use(['table', 'form', 'jquery'], function () {
        let table = layui.table;
        let form = layui.form;
        let $ = layui.jquery;


        let cols = [
            [
                // {type: 'checkbox'},
                {title: '事件', field: 'thing', align: 'center', width: 350, templet: '#user-login'},
                {title: '状态', field: 'sex', align: 'center', templet: '#user-sex'},
                {title: '创建时间', field: 'createTime', align: 'center', width: 350},
                {title: '操作', toolbar: '#user-bar', align: 'center', width: 130}
            ]
        ]

        table.render({
            elem: '#user-table',
            url: '/thing/list',
            page: true,
            cols: cols,
            skin: 'line',
            method: 'post',
            toolbar: '#user-toolbar',
            defaultToolbar: [{
                layEvent: 'refresh',
                icon: 'layui-icon-refresh',
            }, 'filter', 'print', 'exports']
            , done: function (res, curr, count) {
                //如果是异步请求数据方式，res即为你接口返回的信息。
                //如果是直接赋值的方式，res即为：{data: [], count: 99} data为当前页数据、count为数据总长度
                console.log(res);
                //存储页面数据量
                localStorage.setItem("dataLength", res.data.length);
                localStorage.setItem("curr", curr);
                //得到数据总量
                console.log(count);
            },
            limit: 20
        });

        table.on('tool(user-table)', function (obj) {
            if (obj.event === 'remove') {
                window.remove(obj);
            } else if (obj.event === 'edit') {
                window.edit(obj);
            }
        });

        table.on('toolbar(user-table)', function (obj) {
            if (obj.event === 'add') {
                window.add();
            } else if (obj.event === 'refresh') {
                window.refresh();
            } else if (obj.event === 'batchRemove') {
                window.batchRemove(obj);
            }
        });


        //监听操作
        form.on('switch(sexDemo)', function (obj) {
            // layer.tips(this.value + ' ' + this.name + '：' + obj.elem.checked, obj.othis);
            $.ajax({
                url: "/thing/change",
                dataType: 'json',
                data: {id: this.value},
                type: 'post',
                success: function (result) {

                    if (result.code == 0) {
                        layer.msg(result.msg, {
                            icon: 1,
                            time: 1000
                        });
                    } else {
                        layer.msg(result.msg, {
                            icon: 2,
                            time: 1000
                        });
                    }
                    $(".layui-laypage-btn").trigger('click');
                }
            })
        });

        var index1;
        window.add = function () {
            index1 = layer.open({
                type: 1,
                content: $(".opens"),
                // 点击遮罩关闭层这里不应该启用
                area: ['410px', '176px'],
                cancel: function () {
                },
                end: function () {
                },
                success: function () {
                    form.val("xxx", {
                        // 添加前清空值
                        "thingId": ""
                        , "thing": ""
                    });
                }
            });

        }

        window.edit = function (obj) {
            var data = obj.data;
            console.log(obj);
            index1 = layer.open({
                type: 1,
                content: $(".opens"),
                // 点击遮罩关闭层这里不应该启用
                area: ['410px', '176px'],
                // 点击右上角的回调
                cancel: function () {
                    // 模拟点击确定不会跳转页面
                    $(".layui-laypage-btn").trigger('click');
                },
                success: function () {
                    form.val("xxx", {
                        "thingId": data.thingId
                        , "thing": data.thing

                    });
                },
                end: function () {
                    // 模拟点击确定不会跳转页面
                    $(".layui-laypage-btn").trigger('click');
                }
            });
        }

        // 监听弹出层的提交
        form.on('submit(formDemo)', function (data) {
            $.ajax({
                url: "/thing/insert",
                type: "post",
                dataType: "json",
                contentType: "application/json",
                data: JSON.stringify(data.field),
                success: function (res) {
                    if (res.code == 0) {
                        form.val("xxx", {
                            // 成功后清空值避免多次添加
                            "thingId": ""
                            , "thing": ""
                        });
                        //关闭弹出的open层
                        layer.close(index1);
                        layer.msg(res.msg);
                        if (res.msg == "更新成功") {
                            // 由编辑的cancel回调
                        } else {
                            // 添加调到第一页没关系
                            table.reload('user-table', {
                                url: '/thing/list'
                                , where: {}
                                , page: {
                                    curr: 1 //重新从第 1 页开始
                                },
                                method: 'post'
                            });
                        }
                    } else {
                        layer.msg(res.msg);
                    }
                }
            });
            return false;
        });

        window.remove = function (obj) {
            layer.confirm('确定要删除?统计数据也将一起删除！', {
                icon: 3,
                title: '提示'
            }, function (index) {
                $.ajax({
                    url: "/thing/deleteOne",
                    dataType: 'json',
                    data: {id: obj.data.thingId},
                    type: 'delete',
                    success: function (result) {
                        if (result.code == 0) {
                            layer.msg(result.msg, {
                                icon: 1,
                                time: 1000
                            });
                            if (localStorage.getItem("dataLength") == 1) {
                                table.reload('user-table', {
                                    url: '/thing/list'
                                    , where: {}
                                    , page: {
                                        curr: localStorage.getItem("curr") - 1 // 我尝试过只有1页一条删除没问题
                                    },
                                    method: 'post'
                                });
                            } else {
                                $(".layui-laypage-btn").trigger('click');
                            }
                        } else {
                            layer.msg(result.msg, {
                                icon: 2,
                                time: 1000
                            });
                        }
                    }
                })
            });
        }

        // window.batchRemove = function (obj) {
        //   let data = table.checkStatus(obj.config.id).data;
        // }

        window.refresh = function (param) {
            table.reload('user-table');
        }

    });


</script>
</body>
<!-- 弹出层 -->
<div class="opens" style="display: none">
    <form class="layui-form" action="" lay-filter="xxx" style="margin: 15px">
        <div class="layui-form-item" style="display: none">
            <label class="layui-form-label"></span>id</label>
            <div class="layui-input-inline">
                <input type="text" name="thingId" lay-verify="" placeholder="请输入" autocomplete="off"
                       class="layui-input">
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label"><span style="color: red;margin-right: 1px">*</span>事件</label>
            <div class="layui-input-inline">
                <input type="text" name="thing" lay-verify="required" placeholder="请输入" autocomplete="off"
                       class="layui-input" style="border-radius: 4px">
            </div>
        </div>
        <div class="layui-form-item">
            <div class="layui-input-block">
                <button class="layui-btn" lay-submit lay-filter="formDemo"
                        style="font-size: 14px;background-color: #44ca73;border-radius: 5px"><i
                        class="layui-icon layui-icon-ok-circle" style="font-size: 14px"></i>立即提交
                </button>
            </div>
        </div>
    </form>
</html>
